﻿// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// This file is part of CodingEditor.
// Note:	This project is derived from Peter Project
//			(hosted on sourceforge and codeplex)
//
// Copyright (c) 2008-2009, CE Team
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


namespace CE.CustomControls
{
	using System.Drawing;
	using System.Windows.Forms;

	#region Enumerations

	public enum DividerOrientation
	{
		Vertical, Horizontal
	}

	#endregion Enumerations

	public partial class Divider : UserControl
	{
		#region Fields

		Color _color1;
		Color _color2;
		DividerOrientation _orientation;

		#endregion Fields

		#region Constructors

		public Divider()
		{
			InitializeComponent();
			_orientation = DividerOrientation.Horizontal;
            _color1 = Color.DimGray;
            _color2 = Color.White;
		}

		#endregion Constructors

		#region Properties

		public Color Color1
		{
			get { return _color1; }
			set { _color1 = value; }
		}

		public Color Color2
		{
			get { return _color2; }
			set { _color2 = value; }
		}

		public DividerOrientation Orientation
		{
			get { return _orientation; }
			set { _orientation = value; }
		}

		#endregion Properties

		#region Methods

		private void Divider_Paint(object sender, PaintEventArgs e)
		{
			Draw(e.Graphics);
		}

		private void Draw(Graphics graphics)
		{
			Point point1 = new Point();
			Point point2 = new Point();
			int valueToAdd = 0;
			if (_orientation == DividerOrientation.Horizontal) {
				if (Width < Height) {
					int value1 = Width;
					int value2 = Height;
					Swap(ref value1, ref value2);
					Width = value1;
					Height = value2;
				}
				point1 = new Point(0, Height / 2);
				point2 = new Point(Width, Height / 2);
				valueToAdd = 1;
			} else if (_orientation == DividerOrientation.Vertical)   {
				if (Height < Width) {
					int value1 = Width;
					int value2 = Height;
					Swap(ref value1, ref value2);
					Width = value1;
					Height = value2;
				}
				point1 = new Point(Width / 2, 0);
				point2 = new Point(Width / 2, Height);
				valueToAdd = -1;
			}
			graphics.DrawLine(new Pen(new SolidBrush(_color1)),
			                  point1, point2);
			if (valueToAdd == 1) {
				point1.Y += valueToAdd;
				point2.Y += valueToAdd;
			} else if (valueToAdd == -1)   {
				point1.X += valueToAdd;
				point2.X += valueToAdd;
			}
			graphics.DrawLine(new Pen(new SolidBrush(_color2)),
			                  point1, point2);
		}

		private void Swap(ref int value1, ref int value2)
		{
			int appo = value1;
			value1 = value2;
			value2 = appo;
		}

		#endregion Methods
	}
}